<template>
  <div class="goods">
    
    <el-card class="box-card Top Bottom">
      <!-- 头部 -->
      <el-row :gutter="8">
        <el-col :span="8"
          ><div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="tab.query"
              class="input-with-select"
              @input="Search"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="search"
              ></el-button>
            </el-input></div
        ></el-col>
        <el-col :span="16"
          ><div class="grid-content bg-purple-light">
            <el-button type="primary" @click="jump">添加商品</el-button>
          </div></el-col
        >
      </el-row>

      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%" class="Top">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="500">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column label="创建时间" width="159">
          <template slot-scope="scope">
            <div>
              {{ scope.row.add_time | time }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button size="mini" @click="EditGood(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="Del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="tab.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="tab.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        class="Top"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import _ from "lodash";
export default {
  name: "",
  components: {},
  data() {
    return {
      Val: "", //搜索
      //   表格数据
      tableData: [],
      tab: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      //   goods:[],
    };
  },

  created() {
    this.getlist();
  },
  mounted() {},
  methods: {
    async getlist() {
      const res = await this.$http.Goods(this.tab);
      console.log(res);
      this.tableData = res.goods;
      this.total = res.total;
    },
    // 编辑
    EditGood(row) {
      let {
        goods_id,
        goods_name,
        goods_number,
        goods_price,
        goods_state,
        goods_weight,
      } = row;
      console.log(row);
      this.$router.push({
        path: "/goodAdd",
        query: row
        // {
        //   goods_id: goods_id,
        //   goods_name: goods_name,
        //   goods_number: goods_number,
        //   goods_price: goods_price,
        //   goods_state: goods_state,
        //   goods_weight: goods_weight,
        // },
      });
    },
    // 添加商品
    jump() {
      this.$router.push("/goodAdd");
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.tab.pagesize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.tab.pagenum = val;
      this.getlist();
    },
    // 搜索
    search() {
      this.getlist();
    },
    Search: _.throttle(function () {
      this.getlist();
    }, 3000),
    // 删除
    async Del(val) {
      let dels;
      try {
        dels = await this.$confirm(
          "此操作将永久删除该文件, 是否继续?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        );
      } catch (error) {
        dels = error;
      }
      console.log(dels);
      if (dels == "confirm") {
        console.log(val);
        const res = this.$http.DelGoods(val.goods_id);
        // console.log(res);
        this.getlist();
      }
    },
  },
};
</script>
<style scoped lang='scss'>
.goods {
  // width: 100%;
  // height: 100%;
  .Top {
    margin-top: 20px;
  }
  .Bottom {
    margin-bottom: 100px;
  }
}
</style>
